package com.skytech.component.crawler.web;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.parse.JsoupHtmlDownloader;
import com.skytech.component.crawler.parse.ParserLoader;
import com.skytech.component.crawler.parse.SaveDataImpl;
import com.skytech.component.crawler.scratch.*;
import com.skytech.component.crawler.task.CrawlerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CrawlerTaskController {
    @Autowired
    private CrawlerTask crawlerTask;
    @Autowired
    private ParserLoader parserLoader;

    @Autowired
    private SaveDataImpl saveData;

    @Autowired
    private PageContentService pageContentService;

    @Autowired
    private TaskStatusService taskStatusService;
    @Autowired
    private TaskStatusDAO taskStatusDAO;
     @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;
    @Value("${pageNum}")
    private  int pageSize;
    @Autowired
    private TargetRulesDAO targetRulesDAO;

    @Autowired
    private DataTypeDAO dataTypeDAO;

    @Autowired
    private DataFormatDAO dataFormatDAO;

    @GetMapping("crawler/task/parse")
    public String parse(){
        /*crawlerTask.parse();*/
        /* parserLoader.load("039");*/
        return "开始解析";
    }


/*    @GetMapping("crawler/task/parse/{id}")
    public void parse(@PathVariable String id){
         parserLoader.load(id,new JsoupHtmlDownloader(),saveData);

    }*/

    @PostMapping("crawler/task/get")
    public JSONObject getDataByRuleId(@RequestBody JSONObject jsonObject){
      //  Pageable pageable = PageRequest.of(jsonObject.getInteger("page"),pageSize);
        //ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "crawler_news/news/_search",jsonObject, JSONObject.class);
       String ruleId=jsonObject.getJSONObject("query").getJSONObject("match").getString("ruleId");
        String type=targetRulesDAO.findAllById(ruleId).getDataType().trim();
        //ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "bid/bid/_search",jsonObject, JSONObject.class);
        ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + type+"/news/_search",jsonObject, JSONObject.class);

        return pageContentService.getData(responseEntity);
       // return pageContentDAO.findByRuleId(jsonObject.getString("data"),pageable);
    }

 /*   @GetMapping("crawler/task/export/{id}")
    public void exportData(@PathVariable String id){
        List<PageContent> pageContentList=pageContentDAO.findByRuleId(id);
        ExcelTool excelTool = new ExcelTool();
        for (PageContent pageContent : pageContentList) {
            List<String> columns = new ArrayList<>();
            columns.add(pageContent.getSourceUrl());
            columns.add(pageContent.getContent());
            //columns.add(pageContent.getDataItem());
            columns.add(pageContent.getRuleId());
            columns.add(pageContent.getTitle());


            excelTool.print(columns);
        }
        String fileName= UUID.randomUUID().toString()+id;
        excelTool.downloadExcel(filePath,fileName);
    }*/

    @GetMapping("crawler/task/getTaskByStatus")
    public Map<String,Integer> getTaskByStatus(){
        return taskStatusService.findAllByStatus();
    }

    @GetMapping("crawler/task/getTaskById/{id}")
    public List<TaskStatus> getTaskById(@PathVariable String id){
        return taskStatusDAO.findAllByRuleId(id);
    }

    @GetMapping("crawler/task/getAllTask")
    public List<TaskStatus> getAllTask(){
        return taskStatusDAO.findAll();
    }


    @PostMapping("crawler/listDataCountByTypeAndTime")
    public  JSONObject listDataCountByTypeAndTime(@RequestBody JSONObject jsonObject){
         return pageContentService.listDataCountByTypeAndTime(jsonObject);
    }


    @PostMapping("crawler/listDataByTypeAndTime")
    public  JSONObject listDataByTypeAndTime(@RequestBody JSONObject jsonObject){
        return pageContentService.listDataByTypeAndTime(jsonObject);
    }

    @PostMapping("crawler/index/indexIsExist")
    public Boolean indexIsExist(@RequestBody String dataType){
       // ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().getForEntity(server + dataType, JSONObject.class);
       return restTemplateBuilder.build().getForEntity(server + dataType, JSONObject.class).getBody().getJSONObject(dataType).getJSONObject("mappings").isEmpty();
    }

    @PostMapping("crawler/deleteIndex")
    public void deleteIndex(@RequestBody DataType dataType){
        restTemplateBuilder.build().delete(server + dataType.getDataType());
        dataFormatDAO.deleteAll(dataFormatDAO.findByDataType(dataType.getDataType()));
        dataTypeDAO.delete(dataType);
    }








}
